﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.DB;
using System.Data;
using MaxLeaf.Common;

namespace MaxLeaf.DAL.K3
{
    /// <summary>
    /// 折扣政策Dtl
    /// </summary>
    public class ICDisPlyEntryDAL : BaseDAL
    {
        public ICDisPlyEntryDAL()
            : base()
        {
            strTableName = "ICDisPlyEntry";

            strMapKeyFieldName = "FEntryID";
            //hdr.FInterID
            strDestRealKeyFieldName = "FCuryID";

            mblnNeedEditKeyValue = false;
            mblnIsIdentity = true;
            blnMapKeyUseOtherKeyFieldsCond = false;

            strFieldNames = "FInterID,FItemID,FRelatedID,FModel,FAuxPropID,FUnitID,FBegQty,FEndQty,FBegAmt,FEndAmt,FCuryID,FDisType,FAppScale,FValue,FBegDate,FEndDate,FNote,FChecked,FIndex,FB2Emp,FB2EmpCls,FB2CustCls,FB2VipGrp,FFlagSave,FItemID2";
        }
        public override void Update(DBAccess dbOrgCon, DBAccess dbDestCon, string strKeyValue)
        {
            KeyValuePair<string, IDataParameter[]> kvp = dbDestCon.CreateCondParam(new Dictionary<string, object>() { { "TableName", strTableName }, { "OrgKeyValue", strKeyValue } });
            string strSql = string.Format("select DestKeyValue from mkeymap where {0}", kvp.Key);

            string strFEntryID = dbDestCon.GetValue(strSql, kvp.Value);
            if (strFEntryID.Length > 0)
            {

            }
            else
            {
                //FRelatedID=类别id，有主库分发,源和目标的可能不一样
                DataRow drOrgData = dbOrgCon.GetTableDataFirstRow(strTableName, new Dictionary<string, object>() { { strMapKeyFieldName, strKeyValue } });
                DataRow drOrgHdrData = dbOrgCon.GetTableDataFirstRow("ICDisPly", new Dictionary<string, object>() { { "FInterID", drOrgData["FInterID"] } },"FPlyType");

                dictOtherKeyFieldsCond = new Dictionary<string, object>() { 
                    {"FInterID",drOrgData["FInterID"]}
                    ,{"FItemID",drOrgData["FItemID"]}
                    ,{"FRelatedID",drOrgData["FRelatedID"]}
                    ,{"FUnitID",drOrgData["FUnitID"]}
                    ,{"FBegQty",drOrgData["FBegQty"]}
                    ,{"FEndQty",drOrgData["FEndQty"]}
                    ,{"FBegAmt",drOrgData["FBegAmt"]}
                    ,{"FEndAmt",drOrgData["FEndAmt"]}
                    ,{"FBegDate",drOrgData["FBegDate"]}
                    ,{"FEndDate",drOrgData["FEndDate"]}
                };
                dictDestOtherKeyFieldsCond = new Dictionary<string, object>() { 
                    {"FInterID",new ICDisPlyDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData["FInterID"]))}
                    ,{"FItemID", GetFItemId(dbOrgCon, dbDestCon,CvtFunc.ToString(drOrgHdrData["FPlyType"]), CvtFunc.ToString(drOrgData["FItemID"]))}
                    ,{"FRelatedID",new t_SubMessageDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData["FRelatedID"]))}
                    ,{"FUnitID",new t_MeasureUnitDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData["FUnitID"]))}
                    ,{"FBegQty",drOrgData["FBegQty"]}
                    ,{"FEndQty",drOrgData["FEndQty"]}
                    ,{"FBegAmt",drOrgData["FBegAmt"]}
                    ,{"FEndAmt",drOrgData["FEndAmt"]}
                    ,{"FBegDate",drOrgData["FBegDate"]}
                    ,{"FEndDate",drOrgData["FEndDate"]}
                };
            }
            base.Update(dbOrgCon, dbDestCon, strKeyValue);
        }

        private static string GetFItemId(DBAccess dbOrgCon, DBAccess dbDestCon, string strFPlyType, string strOrgFItemID)
        {
            if (strFPlyType.Equals("DisAsm2", StringComparison.InvariantCultureIgnoreCase))
            {
                return new ImInvMasterDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, strOrgFItemID);
            }
            else
            {
                return new t_SubMessageDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, strOrgFItemID);
            }
        }

        protected override void DAL_GetFieldData(DBAccess dbOrgCon, DBAccess dbDestCon, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strFieldName.Equals("FItemID", StringComparison.InvariantCultureIgnoreCase))
            {
                //物料
                DataRow drOrgHdrData = dbOrgCon.GetTableDataFirstRow("ICDisPly", new Dictionary<string, object>() { { "FInterID", drOrgData["FInterID"] } }, "FPlyType");

                drDestData[strFieldName] = GetFItemId(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgHdrData["FPlyType"]), CvtFunc.ToString(drOrgData["FItemID"]));
                
            }
            else if (strFieldName.Equals("FRelatedID", StringComparison.InvariantCultureIgnoreCase))
            {
                //类别
                drDestData[strFieldName] = new t_SubMessageDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName]));

            }
            else if (strFieldName.Equals("FUnitID", StringComparison.InvariantCultureIgnoreCase))
            {
                //单位
                drDestData[strFieldName] = new t_MeasureUnitDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName]));

            }
            else if (strFieldName.Equals("FInterID", StringComparison.InvariantCultureIgnoreCase))
            {
                //hdr.FInterID
                drDestData[strFieldName] = new ICDisPlyDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName])); 

            }
            else
            {
                base.DAL_GetFieldData(dbOrgCon, dbDestCon, strFieldName, drOrgData, drDestData);
            }
        }

    }
}
